/***************************************************************
 * This script demonstrates how to list cabinets using Doofer.
 ****************************************************************/
import org.doofer.*

class CabinetLister2 {
    def connInfo = [:]    // creates a new Map
    final static def DQL = "select object_name from dm_cabinet order by object_name"
    
    CabinetLister2(params) {
        // populate user credentials map
        connInfo.username = params[0]
        connInfo.password = params[1]
        connInfo.repository = params[2]
    }

    def getCabinetNames() {
        def cabinetNames
        new Doofer().getSessionUtil(connInfo).withSession {
            cabinetNames = queryForList(dql: DQL)     // returns a list of map objects
        }
        
        // this returns a list of the object_name entries
        cabinetNames*.object_name
    }
    
    static main(args) {
        final def separator = '-'.multiply(20)
        if(args.size() == 3) {
            try {
                println separator
                def cabinetNames = new CabinetLister2(args).cabinetNames
                println separator
                cabinetNames.each { println "Found cabinet = $it" }
            } catch(Throwable t) {
                println "Error detected\n${t.message}"
            }
        } else {
            println "Usage: groovy CabinetLister username password docbase"
        }
    }
}